<template>
  <div ref="chart" :style="chartStyle"></div>
</template>

<script>
import echarts from 'echarts';

export default {
  name: 'linepoint',
  props: {
    config: {
      required: true,
    },
    chartStyle: {
      require: false,
      type: Object,
      default: () => ({
        height: '100%',
        position: 'absolute',
        width: '100%',
        top: '0px',
        bottom: '0px',
        left: '0px',
      }),
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {

  },
  mounted() {
    this.renderChart();
  },
  methods: {
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    renderChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chart, this.config.theme);
      }
      const option = this.makeOptions(this.config, this.chartData);
      this.chart.clear();
      this.chart.setOption(option);
      setTimeout(() => {
        this.chart.resize();
      }, 100);
    },
    makeOptions() {
      const icon3 = 'image://';
      const icon2 = 'image://';
      const icon1 = 'image://';
      const icon4 = 'image://';
      const myColor = ['#48a6d9', '#f25e7a', '#57bbca', '#566d86', '#48a6d9', '#f25e7a', '#57bbca', '#566d86'];
      const option = {
        // backgroundColor: '#fff',
        tooltip: {
          show: false,
        },
        grid: {
          top: '20%',
          left: '5%',
          right: '5%',
          bottom: '15%',
        },
        xAxis: {
          data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
          // offset: 15,
          axisLine: {
            lineStyle: {
              color: '#3b5872',
              width: '5',
            },
          },
          axisTick: {
            alignWithLabel: true,
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#ccc',
            },
          },
          axisLabel: {
            textStyle: {
              fontSize: 20,
              fontWeight: '600',
              color(param, index) {
                return myColor[index];
              },
            },
          },
        },
        yAxis: {
          min: 0,
          max: 80,
          splitLine: {
            lineStyle: {
              color: '#ccc',
            },
          },
          axisTick: {
            lineStyle: {
              color: '#ccc',
            },
          },
          axisLine: {
            lineStyle: {
              color: '#ccc',
            },
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#ccc',
            },
          },
        },
        series: [{
          type: 'bar',
          z: 1,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0,
                color: 'rgba(219, 219, 219,1)',
              }, {
                offset: 0.3,
                color: 'rgba(219, 219, 219,.3)',
              }, {
                offset: 1,
                color: 'rgba(219, 219, 219,.1)',
              }],
              global: false,
            },
          },
          barWidth: '40',
          data: [43, 40, 60, 50, 60, 40, 50, 60],
        },
        {
          name: '',
          type: 'effectScatter',
          rippleEffect: {
            period: 1,
            scale: 2,
            brushType: 'fill',
          },
          z: 2,
          symbolPosition: 'end',
          symbolSize: 40.6,
          symbolOffset: ['0', '0'],
          itemStyle: {
            normal: {
              shadowColor: 'rgba(0, 0, 0, .3)',
              shadowBlur: 5,
              shadowOffsetY: 3,
              shadowOffsetX: 0,
              color(params) {
                const colorList = ['#57bbca', '#f25e7a', '#48a6d9', '#566d86', '#57bbca', '#f25e7a', '#48a6d9', '#566d86'];
                return colorList[params.dataIndex];
              },
              label: {
                show: true,
                fontSize: 24,
                position: 'top',
                distance: -80,
                formatter: '{c}',
                fontWeight: '600',
              },
            },
          },
          data: [{
            value: 43,
            symbol: icon1,
          }, {
            value: 40,
            symbol: icon2,
          }, {
            value: 60,
            symbol: icon3,
          }, {
            value: 50,
            symbol: icon4,
          }, {
            value: 60,
            symbol: icon1,
          }, {
            value: 40,
            symbol: icon2,
          }, {
            value: 50,
            symbol: icon3,
          }, {
            value: 60,
            symbol: icon4,
          }],
        },
        {
          name: 'A',
          type: 'line',
          z: 1,
          // symbol: 'roundRect',
          lineStyle: {
            normal: {
              width: 2,
              color: '#fff',
            },
          },
          symbolSize: [0, 0],
          data: [43, 40, 60, 50, 60, 40, 50, 60],
        },

        ],
      };
      return option;
    },
  },
  watch: {
    config: {
      deep: true,
      handler() {
        if (this.config.needResize) {
          this.config.needResize = false;
          this.handleResize();
        }
      },
    },
  },
};
</script>

<style scoped lang="stylus"></style>
